package com.kkd.kkdapp.adapter;

import java.util.List;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.kkd.kkdapp.R;
import com.kkd.kkdapp.activity.KKDWebActivity;
import com.kkd.kkdapp.http.entity.LendData;
import com.kkd.kkdapp.http.manager.HttpCallBack;
import com.kkd.kkdapp.http.manager.KkdHttpMagager;
import com.kkd.kkdapp.listener.OnSingleClickListener;
import com.nostra13.universalimageloader.core.ImageLoader;

public class FindOtherListAdapter extends BaseAdapter {

	private Context mContext;
	private List<LendData> mList;
	private LayoutInflater inflater;

	public FindOtherListAdapter(Context context, List<LendData> list) {
		mContext = context;
		mList = list;
		inflater = LayoutInflater.from(mContext);
	}

	@Override
	public int getCount() {
		return mList.size();
	}

	@Override
	public Object getItem(int position) {
		return mList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		final ViewHolder holder;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = inflater.inflate(R.layout.item_otherlist_find,
					parent, false);
			holder.iv_other = (ImageView) convertView
					.findViewById(R.id.iv_other);
			holder.tv_title_other = (TextView) convertView
					.findViewById(R.id.tv_title_other);
			holder.tv_summary_other = (TextView) convertView
					.findViewById(R.id.tv_summary_other);
			holder.rel_other = convertView.findViewById(R.id.rel_other);
			holder.line_top = convertView.findViewById(R.id.line_top);
			holder.area_top = convertView.findViewById(R.id.area_top);
			holder.point = convertView.findViewById(R.id.point);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		// if (mList.size() == 1) {
		// holder.line_top.setVisibility(View.VISIBLE);
		// holder.area_top.setVisibility(View.GONE);
		// } else {
		// if ((position - 1) % 3 == 0) {
		// holder.line_top.setVisibility(View.VISIBLE);
		// holder.area_top.setVisibility(View.GONE);
		// } else {
		// holder.line_top.setVisibility(View.GONE);
		// holder.area_top.setVisibility(View.VISIBLE);
		// }
		// }
		if (position == 0) {
			holder.line_top.setVisibility(View.GONE);
			holder.area_top.setVisibility(View.VISIBLE);
		} else {
			holder.line_top.setVisibility(View.VISIBLE);
			holder.area_top.setVisibility(View.GONE);
		}
		if (mList.get(position).getNumber() > 0) {
			holder.point.setVisibility(View.VISIBLE);
		} else {
			holder.point.setVisibility(View.GONE);
		}
		// ImageLoader.getInstance().displayImage(
		// mList.get(position).getLendImageUrl(), holder.iv_other);
		holder.tv_title_other.setText(mList.get(position).getLendTitle());
		holder.tv_summary_other.setText(mList.get(position).getLendDesc());
		holder.rel_other.setOnClickListener(new OnSingleClickListener() {

			@Override
			public void onSingleClick(View v) {
				String url = mList.get(position).getLendUrl();
				if (!TextUtils.isEmpty(url)) {
					if (url.indexOf("http") < 0) {
						try {
							ComponentName componet = new ComponentName(
									"com.kkd.kkdapp", url);
							Intent intent = new Intent();
							intent.setComponent(componet);
							intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
							mContext.startActivity(intent);
						} catch (Exception e) {
							e.printStackTrace();
							// 保证不崩
							// ((BaseActivity)mContext).showToast("请更新APP体验");
						}
					} else {
						Intent intent = new Intent(mContext,
								KKDWebActivity.class);
						intent.putExtra("url", url);
						intent.putExtra("noStartActivity", true);
						intent.putExtra("titleName", mList.get(position)
								.getLendTitle());
						mContext.startActivity(intent);
					}
					holder.point.setVisibility(View.GONE);
					KkdHttpMagager.getInstance().reportNewImg(
							mList.get(position).getImgId(), "findList",
							new HttpCallBack() {

								@Override
								public void onSuccess(int statusCode,
										Object responseBody) {
									// TODO Auto-generated method stub

								}

								@Override
								public void onFailure(int statusCode,
										String desc) {
									// TODO Auto-generated method stub

								}
							});
				}
			}
		});
		return convertView;
	}

	class ViewHolder {
		View rel_other, line_top, area_top, point;
		ImageView iv_other;
		TextView tv_title_other, tv_summary_other;
	}
}
